import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
import 'package:intl/intl.dart';

// void main() => runApp(MyApp());
//
// class MyApp extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) {
//     return MaterialApp(
//       title: '任务详情',
//       theme: ThemeData(
//         primarySwatch: Colors.blue,
//         visualDensity: VisualDensity.adaptivePlatformDensity,
//       ),
//       home: TaskDetailPage(),
//     );
//   }
// }

class TaskDetailPage extends StatefulWidget {
  @override
  _TaskDetailPageState createState() => _TaskDetailPageState();
}

class _TaskDetailPageState extends State<TaskDetailPage> {
  final TextEditingController _commentController = TextEditingController();
  double _downloadProgress = 0.0;

  // 模拟数据
  final Map<String, dynamic> _taskData = {
    'title': 'Flutter项目需求文档编写',
    'status': '进行中',
    'owner': '张小明',
    'deadline': DateTime(2024, 3, 15),
    'description': '完成Flutter项目需求文档的编写工作，包括功能模块划分、接口定义和UI原型设计。需要与产品团队密切配合，确保需求准确性。',
    'attachments': [
      {'name': '需求文档模板.docx', 'size': '2.5MB', 'type': 'word'},
      {'name': 'UI原型图.zip', 'size': '8.7MB', 'type': 'zip'},
    ],
    'comments': [
      {'user': '李芳', 'time': DateTime(2024, 2, 28, 14, 30), 'content': '初步模板已上传'},
      {'user': '王刚', 'time': DateTime(2024, 2, 28, 15, 15), 'content': '接口部分需要补充'},
    ]
  };

  void _simulateDownload() async {
    for (int i = 0; i <= 100; i += 10) {
      await Future.delayed(Duration(milliseconds: 200));
      setState(() => _downloadProgress = i / 100);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: GFAppBar(
        title: Text('任务详情'),
        actions: [
          GFIconButton(
            icon: Icon(Icons.more_vert),
            onPressed: () {},
            type: GFButtonType.transparent,
          )
        ],
      ),
      body: ListView(
        padding: EdgeInsets.all(4),
        children: [
          // 任务标题
          Padding(
            padding: const EdgeInsets.only(left: 14, right: 14, bottom: 14, top: 14),
            child: Text(
              _taskData['title'],
              style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
            ),
          ),

          // 状态标签
          GFCard(
            elevation: 2,
            content: GFListTile(
              icon: Icon(Icons.circle, color: Colors.green, size: 16),
              title: Text('当前状态'),
              subTitle: GFBadge(
                text: _taskData['status'],
                color: Colors.green,
                shape: GFBadgeShape.standard,
              ),
            ),
          ),

          // 负责人和截止时间
          GFCard(
            elevation: 2,
            content: Column(
              children: [
                GFListTile(
                  icon: Icon(Icons.person_outline),
                  title: Text('负责人'),
                  subTitle: Text(_taskData['owner']),
                ),
                Divider(),
                GFListTile(
                  icon: Icon(Icons.access_time),
                  title: Text('截止时间'),
                  subTitle: Text(
                    DateFormat('yyyy-MM-dd HH:mm').format(_taskData['deadline']),
                    style: TextStyle(color: Colors.red),
                  ),
                ),
              ],
            ),
          ),

          // 任务描述
          GFCard(
            elevation: 2,
            content: Padding(
              padding: EdgeInsets.all(4),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('任务描述', style: TextStyle(fontWeight: FontWeight.bold)),
                  SizedBox(height: 10),
                  Text(_taskData['description'], style: TextStyle(color: Colors.grey[600])),
                ],
              ),
            ),
          ),

          // 附件下载
          GFCard(
            elevation: 2,
            content: Padding(
              padding: EdgeInsets.all(4),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('相关附件 (${_taskData['attachments'].length})',
                      style: TextStyle(fontWeight: FontWeight.bold)),
                  ..._taskData['attachments'].map((file) => GFListTile(
                    padding: const EdgeInsets.symmetric(),
                    margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 2),
                    icon: _getFileIcon(file['type']),
                    title: Text(file['name']),
                    subTitle: Text(file['size']),
                    description: GFButton(
                      text: '下载',
                      size: GFSize.SMALL,
                      onPressed: _simulateDownload,
                    ),
                  )).toList(),
                  _downloadProgress > 0 ? GFProgressBar(
                    percentage: _downloadProgress,
                    lineHeight: 8,
                    backgroundColor: Colors.black26,
                    progressBarColor: GFColors.PRIMARY,
                  ) : SizedBox(),
                ],
              ),
            ),
          ),

          // 评论区域
          GFCard(
            elevation: 2,
            content: Padding(
              padding: EdgeInsets.all(4),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('评论 (${_taskData['comments'].length})',
                      style: TextStyle(fontWeight: FontWeight.bold)),
                  SizedBox(height: 10),
                  ..._taskData['comments'].map((comment) => GFListTile(
                    padding: const EdgeInsets.symmetric(),
                    margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 2),
                    avatar: GFAvatar(
                      backgroundImage: AssetImage('assets/user.png'),
                    ),
                    title: Text(comment['user']),
                    subTitle: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(comment['content']),
                        Text(
                          DateFormat('MM-dd HH:mm').format(comment['time']),
                          style: TextStyle(color: Colors.grey, fontSize: 12),
                        ),
                      ],
                    ),
                  )).toList(),
                  SizedBox(height: 10),
                  Row(
                    children: [
                      Expanded(
                        child: GFTextField(
                          controller: _commentController,
                          decoration: InputDecoration( // 20250320 修饰在这里
                            hintText: "请输入评论",
                            hintMaxLines: 1, // 控制提示语最大行数‌
                            border: InputBorder.none, // 隐藏默认边框‌
                            filled: true,
                            fillColor: Colors.grey[100],
                          ),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 10),
                  Row(
                    children: [
                      // SizedBox.expand(
                      //   child: ElevatedButton(
                      //     onPressed: () {},
                      //     child: const Text("按钮占满父容器"),
                      //   ),
                      // ),
                      Expanded( // 添加expand，会沾满宽度
                        child: GFButton(
                          text: '发送',
                          type: GFButtonType.outline,
                          onPressed: () {},
                        ),
                      ),

                    ],
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _getFileIcon(String type) {
    Map<String, IconData> icons = {
      'word': Icons.description,
      'zip': Icons.archive,
      'pdf': Icons.picture_as_pdf,
    };
    return Icon(
      icons[type] ?? Icons.insert_drive_file,
      color: GFColors.PRIMARY,
    );
  }
}
